package com.chai.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.chai.constant.MessageConstant;
import com.chai.dao.MemberDao;
import com.chai.dao.OrderDao;
import com.chai.dao.OrderSettingDao;
import com.chai.pojo.Member;
import com.chai.pojo.Order;
import com.chai.pojo.OrderSetting;
import com.chai.service.OrderService;
import com.chai.utils.DateUtils;
import com.chai.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {

    @Autowired
    OrderDao orderDao;

    @Autowired
    OrderSettingDao orderSettingDao;

    @Autowired
    MemberDao memberDao;

    @Override
    public Result saveOrder(Map map) throws Exception {

        String orderDateStr = (String) map.get("orderDate");
        String telephone = (String) map.get("telephone");
        String name = (String) map.get("name");
        String sex = (String) map.get("sex");
        String idCard = (String) map.get("idCard");
        String setmealIdstr = (String) map.get("setmealId");
        Integer setmealId = Integer.parseInt(setmealIdstr);
        Date orderDate = DateUtils.parseString2Date(orderDateStr);

        //1.预约设置不存在，或可预约人数是0，无法预约。
        OrderSetting orderSetting = orderSettingDao.findByOrderDate(orderDate);
        if (orderSetting == null || orderSetting.getNumber() == 0) {
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        } else {
            //2.如果已预约人数大于等于可预约人数，无法预约。
            int number = orderSetting.getNumber();
            int reservations = orderSetting.getReservations();
            if (reservations >= number) {
                return new Result(false, MessageConstant.ORDER_FAIL);
            }
        }

        //3.查询会员，会员不存在进行快速注册。会员存在可以预约

        Member member = memberDao.getNumberByTelephone(telephone);
        if (member == null) {
            member = new Member();
            member.setName(name);
            member.setSex(sex);
            member.setIdCard(idCard);
            member.setPhoneNumber(telephone);
            member.setRegTime(new Date());
            memberDao.save(member);
        } else {
            //判断是否重复预约
            Map mapParam = new HashMap();
            mapParam.put("memberId", member.getId());
            mapParam.put("orderDate", orderDate);
            mapParam.put("setmealId", setmealId);
            int count = orderDao.findByCondition(mapParam);
            if (count > 0) {
                return new Result(false, MessageConstant.HAS_ORDERED);
            }
        }
        //开始下单并预约
        Order orderObj = new Order();
        orderObj.setMemberId(member.getId());
        orderObj.setOrderDate(orderDate);
        orderObj.setOrderType(Order.ORDERTYPE_WEIXIN);
        orderObj.setOrderStatus(Order.ORDERSTATUS_NO);
        orderObj.setSetmealId(setmealId);
        orderDao.save(orderObj);//主键回填

        orderSetting.setReservations(orderSetting.getReservations()+1);
        orderSettingDao.updateReservations(orderSetting);

        return new Result(true,MessageConstant.ORDER_SUCCESS,orderObj);
    }

    @Override
    public Map findById(Integer id) throws Exception {
        Map map = orderDao.findById(id);
        Date date = (Date)map.get("orderDate");
        map.put("orderDate",DateUtils.parseDate2String(date));
        return orderDao.findById(id);
    }
}
